<script>
    // 类方法(静态方法) all

    // 创建三个Promise
    const p1 = new Promise((resolve, reject) => {
        setTimeout(() => {
            // resolve("p1 resolve")
            reject("p1 reject error")
        }, 3000)
    })
    const p2 = new Promise((resolve, reject) => {
        setTimeout(() => {
            // resolve("p2 resolve")
            reject("p2 reject error")
        }, 2000)
    })
    const p3 = new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve("p3 resolve")
        }, 5000)
    })

    // const p4 = 110;

    Promise.all = function (promises) {
        return new Promise((resolve, reject) => {
            let result = [];
            let index = 0;
            let len = promises.length;
            if (len === 0) {
                resolve(result)
                return;
            }
            for (let i = 0; i < len; i++) {
                // promises[i]
                Promise.resolve(promises[i]).then(data => {
                    result[i] = data;
                    index++;
                    if (index === len) {
                        resolve(result)
                    }
                }).catch(err => {
                    reject(err)
                })
            }
        })
    }

    // 类方法(静态方法) all
    // all的作用：所有promise都成功后，得到所有成功后的promise结果
    //    如果有一个先失败了，直接得到最先失败promise的结果
    Promise.all([p1, p2, p3]).then(res => {
        console.log(res);
    }).catch(err => {
        console.log(err);
    })
</script>